namespace NHCodeFirst.FluentMapper
{
    using System;
    using Enums;
    using HbmModel;

    public class IdMapper : TypeAndColumnMapper<IdMapper>
    {
        private readonly Id id;

        internal IdMapper(Id id) : base(id.Type,id.Column)
        {
            this.id = id;
            child = this;
        }

        public IdMapper Generator(IdGenerator generator, params Param[] parameters)
        {
            id.Generator.Class = EnumsToHbmString.GetGenerator(generator);
            foreach (var generatorParameter in parameters)
                id.Generator.Params.Add(generatorParameter.Name, generatorParameter.Value);
            return this;
        }

        public IdMapper UnsavedValue(object value)
        {
            id.UnsavedValue = value == null ? null : value.ToString();
            return this;
        }

        public IdMapper Access(Access access, Naming? namingStrategy = null)
        {
            return Access(access.ToString().ToLower(), namingStrategy);
        }

        public IdMapper Access<T>(Naming? namingStrategy = null)
        {
            return Access(typeof(T).FullName, namingStrategy);
        }

        public IdMapper Access(Type type, Naming? namingStrategy = null)
        {
            return Access(type.FullName, namingStrategy);
        }

        private IdMapper Access(string access, Naming? naming)
        {
            id.Access = access;
            if (naming.HasValue)
                id.Access += "." + EnumsToHbmString.GetAccessNamingStrategy(naming.Value);
            return this;
        }
    }
}